Page({
  data: {
    title: '',
    description: '',
    date: ""
  },
  onLoad: function () {
    this.setData({
      date: this.getToday()
    })
  },
  onTitleInput: function(e) {
    this.setData({ title: e.detail.value });
  },
  onDescriptionInput: function(e) {
    this.setData({ description: e.detail.value });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  saveTodo: function() {
    if (!this.data.title.trim()) {
      wx.showToast({
        title: '标题不能为空',
        icon: 'none'
      });
      return;
    }
    const todos = wx.getStorageSync('dataList') || [];
    const newTodo = {
      id: Date.now().toString(),
      title: this.data.title,
      completed: false,
      date: this.data.date,
      description: this.data.description
    };
    todos.push(newTodo);
    wx.setStorageSync('dataList', todos);
    wx.navigateBack();
  },
  getToday: function() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
  }
});